// import 'package:euromillions/tools/EJ_config.dart';
// import 'package:euromillions/tools/EJ_emsBalls.dart';
// import 'package:euromillions/tools/models/EJ_DataViewModels.dart';
import 'package:flutter/material.dart';
import 'package:hexcolor/hexcolor.dart';
import 'package:intl/intl.dart';
// import 'package:kerala_lottery/Results/KaL_resultDetailPage.dart';
// import 'package:kerala_lottery/tools/models/KL_keralaLottoDataModel.dart';

import '../tools/FL_emsBalls.dart';
import '../tools/KaL_Config.dart';
import '../tools/KaL_drawNumbersView.dart';
import '../tools/models/KL_keralaLottoDataModel.dart';
import '../tools/models/KL_keralaLottoModel.dart';
import '../tools/models/KaL_LottoModel.dart';
import '../tools/models/KaL_DataViewModels.dart';
import 'EJ_ResultDetailPage.dart';
import 'KaL_resultDetailPage.dart';
// import 'EJ_ResultDetailPage.dart';

class EJResultPageVC extends StatefulWidget {
  const EJResultPageVC({super.key});

  @override
  State<EJResultPageVC> createState() => _EJResultPageVCState();
}

class _EJResultPageVCState extends State<EJResultPageVC> {
  ///列表数据
  late List _results = [];

  ///
  void _goToDetailPage(List prizes) {
    Navigator.push(
        context,
        MaterialPageRoute(
            builder: (context) => KalResultdetailpage(
                  prizes: prizes,
                )));
  }

  ///
  _configData() {
    var models = KL_keralaLottoDataModel().allDataModels;
    for (var element in models) {
      _results.add(element);
    }

    setState(() {});
  }

  @override
  void initState() {
    // TODO: implement initState
    super.initState();

    _configData();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        // title: const Text(
        //   'Draws',
        //   style: TextStyle(
        //     fontWeight: FontWeight.w600,
        //     fontSize: 18,
        //   ),
        // ),
        title: Image.asset(
          'assets/sys_imgs/DearLottery Logo.png',
          width: 160,
          height: 50,
        ),
        backgroundColor: Colors.white,
        foregroundColor: KaL_Config.KaL_MainColor,
        centerTitle: true,
      ),
      body: Container(
        color: KaL_Config.KaL_pageBgColor,
        child: ListView.builder(
            itemCount: _results.length,
            itemBuilder: (context, index) {
              KL_keralaLottoModel model = _results[index];
              return resultListCell(model);
            }),
        // child: RefreshIndicator(
        //   onRefresh: () async {
        //     // NL_pageCount = 1;
        //     // NL_refreshData();
        //     debugPrint('Refresh');
        //   },
        //   child: ListView.builder(
        //       itemCount: _results.length,
        //       itemBuilder: (context, index) {
        //         KaL_LottoModel model = _results[index];
        //         // KaL_LottoModel model = KaL_LottoModel();
        //         return resultCell(model);
        //       }),
        // ),
      ),
    );
  }

  Widget resultListCell(KL_keralaLottoModel model) {
    var gameName = model.game_name;
    var drawCode = model.draw_code;
    var title = '$gameName($drawCode)';

    var draw_datetime_utc = model.draw_datetime_utc;
    DateTime drawDateTime = DateTime.parse(draw_datetime_utc + '+0000');
    var dateTimeString = DateFormat('dd/MMM/yyyy').format(drawDateTime);

    var numbersMap = {};
    List balls = [];
    PrizeModel prizeModel = model.prizes![0];
    List ticketNumbers = prizeModel.ticket_numbers!;
    if (ticketNumbers.isNotEmpty) {
      String numberStr = ticketNumbers[0];
      // // 使用 split 方法将字符串转换为字符串数组
      List<String> charArray = numberStr.split('');
      // numbersMap = charArray;
      for (var i = 0; i < charArray.length; i++) {
        var ball = {
          'number': charArray[i],
          'isRight': false,
          'isNormal': true
        }; //isNormal代表是否对比中奖号码，true不用对比，false需要对比。isRight代表是否中奖号码
        balls.add(ball);
      }
      var title = prizeModel.rank_title + ' Prize';
      numbersMap = {'title': title, 'balls': balls};
    }

    return Container(
      height: 140,
      child: GestureDetector(
        onTap: () {
          debugPrint('result');
          _goToDetailPage(model.prizes!);
        },
        child: Container(
          margin: const EdgeInsets.fromLTRB(15, 15, 15, 1),
          decoration: const BoxDecoration(
            color: Colors.white,
            borderRadius: BorderRadius.all(Radius.circular(10)),
          ),
          child: Column(children: [
            Container(
              margin: EdgeInsets.only(left: 10, right: 10),
              height: 40,
              // color: Colors.yellow,
              child: Row(children: [
                Text(title,
                    style: TextStyle(
                      fontWeight: FontWeight.w700,
                      fontSize: 14,
                      color: HexColor('#D32F7D'),
                    )),
                const Spacer(),
                Text(
                  dateTimeString,
                  style: TextStyle(
                      fontWeight: FontWeight.w700,
                      fontSize: 15,
                      color: HexColor('#999999')),
                ),
              ]),
            ),
            Expanded(
                child: Container(
              width: double.infinity,
              child: Row(
                children: [
                  Expanded(
                      child: Container(
                    // color: Colors.red,
                    alignment: Alignment.centerLeft,
                    child: KalDrawnumbersview(data: numbersMap),
                  )),
                  assetTagView(),
                ],
              ),
            )),
          ]),
        ),
      ),
    );
  }

  Widget assetTagView() {
    return Container(
      alignment: Alignment.center,
      height: double.infinity,
      width: 30,
      decoration: const BoxDecoration(
          color: Colors.white,
          borderRadius: BorderRadius.only(
            topRight: Radius.circular(10),
            bottomRight: Radius.circular(10),
          )),
      child: Image.asset(
        'assets/sys_imgs/EJ_union.png',
        width: 8.0,
        height: 13.0,
      ),
    );
  }
}
